import { FlowData } from ".."
import { reactive } from "vue";

const getParentMap = ()=>{
    const parentMap:Map<string,FlowData> = reactive(new Map());
    const loop = (data:FlowData, key:string, callback:(data:FlowData)=>void ,last?:FlowData)=>{//递归查找节点并执行 callback：操作 key：id data:数据
        if(last){//顺便记录parent
            parentMap.set(data.id,last)
            // parentMap[data.id] = last
        }
        // console.log(data,key,1)
        if (data.id == key) {
            console.log(data,key)
            callback(data)
            return
        }
        if (data.branches) {
            for (let i = 0; i < data.branches.length; i++) {
                parentMap.set(data.branches[i].id,data)//顺便记录parent
                // parentMap[data.branches[i].id] = data
                if (data.branches[i].id == key) {
                    callback(data.branches[i])
                    // callback(data.branches[i], i, data)
                    return 
                }
                if (data.branches[i].nextNode) {
                    loop(data.branches[i].nextNode as FlowData, key, callback,data.branches[i])
                }
            }
            // this.loop(data.branches, key, callback)
        }
        if(data.nextNode){
            loop(data.nextNode, key, callback,data)
        }
    } 
    return {parentMap,loop}
}


// const delItem = (item:FlowData)=>{
//     loop(d,item.id,(data)=>{
//         let parentId = parentMap[item.id].id
//         if(data.type == "branch"){
//             loop(d,parentId,(d)=>{
//                 if(d.branches.length>2){
//                     for(let i in d.branches){
//                         let b = d.branches[i];
//                         if(b.id == item.id){
//                             d.branches.splice(i,1)
//                         }
//                     }
//                 }else{
//                     loop(d,parentMap[d.id].id,(parent)=>{
//                         parent.nextNode = d.nextNode
//                     })
//                     // d.nextNode = data.nextNode
//                     // delete d.branches
//                 }
//             })
//         }
// }
export {
    getParentMap
}